<template>
	<view>
		<view class="add-address">
			<!-- 添加收货地址按钮 -->
			<view class="btn-box" v-if="JSON.stringify(address) =='{}'">
				<button class="mini-btn" type="primary" size="mini" @click="chooseAddress">请选择收货地址</button>
			</view>
			<!-- 收货人的详细信息 -->
			<view class="address-info" v-else @click="chooseAddress">
				<view class="username-info">
					<view>
						<text>收货人：</text>
						<text>{{address.userName}}</text>
					</view>
					<uni-icons type="right" size="20"></uni-icons>
				</view>
				<view class="phone-info">
					<text>联系电话：</text><text>{{address.telNumber}}</text>
				</view>
				<view class="address-detail">
					<text>详细地址：</text>
					<text>{{addressJoin}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		name: "my-address",
		data() {
			return {
				// address: {}
			};
		},
		methods: {
			...mapMutations('m_user', ['updateAddress']),
			async chooseAddress() {
				const [err, success] = await uni.chooseAddress().catch(err => err)
				if (err === null && success.errMsg === "chooseAddress:ok") {
					// this.address = success
					this.updateAddress(success)
				}
				// 如果用户单击取消授权了,我们让他重新授权
				if (err && (err.errMsg === "chooseAddress:fail auth deny" || err.errMsg ===
						'chooseAddress:fail authorize no response')) {
					this.reAuth()
				}
			},
			// 重新授权方法
			async reAuth() {
				const [err, confirmRresult] = await uni.showModal({
					content: '您没有授权，请授权',
					confirmText: '确认',
					cancleText: '取消'
				})
				if (err) return
				if (confirmRresult.cancle) return uni.$showMsg('您取消了授权')
				if (confirmRresult.confirm) return uni.openSetting({
					success: (settingResult) => {
						if (settingResult.authSetting['scope.address']) {
							return uni.$showMsg('授权成功，请选择地址！')
						}
						if (!settingResult.authSetting['scope.address']) {
							return uni.$showMsg('您取消了地址授权！')
						}
					}
				})
			}
		},
		// 计算属性,拼接地址
		computed: {
			...mapState('m_user', ['address']),
			...mapGetters('m_user', ['addressJoin'])
			// addressJoin() {
			// 	if (this.address.provinceName == '') return ''
			// 	return this.address.provinceName + this.address.cityName + this.address.countyName + this.address.detailInfo
			// }
		}
	}
</script>

<style lang="scss">
	.add-address {
		width: 100%;
		background-color: #fff;
		border-bottom: 1px solid #DEDEDE;

		.btn-box {
			height: 80rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;
		}
	}

	.address-info {
		font-size: 14px;

		.username-info {
			width: 100%;
			height: 50rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.phone-info {
			height: 50rpx;
			line-height: 50rpx;
		}
	}
</style>